	#include<reg52.h>
int m= 0,n =0,o =0,scan;
 /*   */
unsigned char fu=0;
unsigned char key;
long d=0,a=0, c=0 ;
int p=0,q=0;
unsigned char tiao[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x71,0x79,0x5e,0x39,0x7c,0x77};
unsigned char wei[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};


void delay(int x)
{
 while(x--)
 {
  }
}

void sheng()
{
P3=0x00;
delay(10000);
P3=0xff;
}

void playerror()
{
 P1=0x00;
 P0=0x79;
 sheng();
}

unsigned char keyscan()
{
unsigned char x,y,k,l,heng,shu;
P2=0xf0;
heng=P2;
if(heng!=0xf0)
{
 delay(10000);
 heng=P2;
 if(heng!=0xf0)
 {
   switch(heng)
   {
    case 0xe0: x=3; break;
    case 0xd0: x=2; break;
    case 0xb0: x=1; break;
    case 0x70: x=0; break;
   }
   P2=0x0f;
   shu=P2;
   switch(shu)
    {
    case 0x0e: y=0; break;
    case 0x0d: y=1; break;
    case 0x0b: y=2; break;
    case 0x07: y=3; break;
	}
	l=4*x+y;
 	switch(l)
  {
   case 0: k=10; break;
   case 1: k=1; break;
   case 2: k=2; break;
   case 3: k=3; break;
   case 4: k=11; break;
   case 5: k=4; break;
   case 6: k=5; break;
   case 7: k=6; break;
   case 8: k=0; break;
   case 9: k=7; break;
   case 10: k=8; break;
   case 11: k=9; break;
   case 12: k=12; break;
   case 13: k=13; break;
   case 14: k=14; break;
   case 15: k=15; break;
  }

	while(shu!=0x0f)
	{
	 shu=P2;
	}
   scan=1;
 }
}
return(k);
}


void play(long v)
{
int i,j;
if(v<10&&v>=0)
{
 P1=0x7f;
 P0=tiao[v];
}
else if(v<100&&v>9)
{
  for(i=0;i<2;i++)
  {	
   j=v%10;
   v=v/10;
   P1=wei[i];
   P0=tiao[j] ;
  }
}
else if(v<1000&&v>99)
{
  for(i=0;i<3;i++)
  {	
   j=v%10;
   v=v/10;
   P1=wei[i];
   P0=tiao[j];
  }
}
else if(v<10000&&v>999)
{
  for(i=0;i<4;i++)
  {	
   j=v%10;
   v=v/10;
   P1=wei[i];
   P0=tiao[j];
  }
}
else if(v<100000&&v>9999)
{
  for(i=0;i<5;i++)
  {	
   j=v%10;
   v=v/10;
   P1=wei[i];
   P0=tiao[j];
  }
}
else if(v<1000000&&v>99999)
{
  for(i=0;i<6;i++)
  {	
   j=v%10;
   v=v/10;
   P1=wei[i];
   P0=tiao[j];
  }
}
else if(v<10000000&&v>999999)
{
  for(i=0;i<7;i++)
  {	
   j=v%10;
   v=v/10;
   P1=wei[i];
   P0=tiao[j];
  }
}
else if(v<100000000&&v>9999999)
{
  for(i=0;i<8;i++)
  {	
   j=v%10;
   v=v/10;
   P1=wei[i];
   P0=tiao[j];
  }
}
}

void shuru(int key)
{
  
  if(key>=12&&key<16)
   {
     if(key==14&&m==0&&n==0)
	 {
	 p=1;
	 }
	 else if(key==14&&m==1)
	 {
	 q=1;
	 o=1;
	 }
	 else
	 {
	 fu=key;
	 m=1;
	 }
	       
    }
  else if(key==11)
	{
	 if(m==0)
	 {
	 a=a/10;
	 }
	 else 
	 {
	 c=c/10;
	 } 
	}
  else
    {
	  if(m==0&&key<10)
	   {
    	  a=a*10+key;
		  n=1;		 
     	}
	   else if(m==1&&key<10)
	    {
     	    c=c*10+key;
			o=1;			
		 }
	}	
		  	  	  		  	 
}

void shuruxs()
{
    if(n==0&&m==1)
	{
	while(1)
	{
	  	playerror();
	}

	}
    if(m==0)
	     {
		 if(a>99999999||a<-9999999)
		 {
		 playerror();
		 }
		 else
		 {
		 if(p==1)
		 {
		   P1=0xfe;
           P0=0x40;	
		   play(a);
		   delay(100) ;
		 }
		 else
		 {
		  play(a);
		 }
		  }
		 }
	else if(m==1&&o==0)
		 {
		 P1=0x7f;
		 P0=tiao[fu];
		 }
	else if(m==1&&o==1)
		 {
		if(c>99999999||c<-9999999)
		 {
		 playerror();
		 }
		 else
		 {
		if(q==1)
		 {
		   P1=0xfe;
           P0=0x40;	
		   play(c);
		   delay(100) ;
		 }
		 else
		 {
		  play(c);
		 }
		  }
		 }
}


void jisuan()
{
       if(fu==15)
	     {
	       d=a+c;
  	     }
	    else if(fu==14)
	     {
    	   d=a-c;
	     }
	    else if(fu==13)
   	     {
    	   d=a*c;
	     }
    	else if(fu==12)
	     {
    	   d= a/c;
	     }
		 while(1)
		 { 
		 if(d>=0)
		 {	
	      if(d>99999999||d<0)
		  {
		  playerror();
		  }
		  else
		 {
		  play(d);
		  }
		  }

		  else if(d<0)
		  {
		  if(d<-9999999)
		  {
		   playerror();
		  }
		  else
		  {
		   long t;
		   t=0-d;		   
		   P1=0xfe;
                   P0=0x40;	  
		   play(t);
		   delay(100);			 
		  }
		  }		  
		  		  		  		  
		 }
}
void main()
{
while(1)
{
  	 
  key=keyscan();
  if(scan==1)
  {
  sheng();
  shuru(key);
  scan=0; 
  } 
  shuruxs();
  if(key==10)
	  {
          if(m==1&&n==1&&o==1)
          {
	   if(fu==12&&c==0&&o==1)
	   {
	   while(1)
	   {
	   playerror();
	   }
	   }
	   if(p==1)
	   {
		a=a*-1;
	   }
	   	if(q==1)
		{
		c=c*-1;
		}
		jisuan();
          }
         else
           {
              while(1)
               {
                 playerror();
               }
           }
           
 }
}	  
}